<?php
/**
 * communit.as
 * @copyright (C)2008 J. William McCarthy, All Rights Reserved.
 * @category communitas
 * @package communitas
 * @author J. William McCarthy
 * @link http://communit.as communit.as
 * @license http://communit.as/docs/license License
 */

/**
 *
 * @package communitas
 * @subpackage forum_models
 * @license http://communit.as/docs/license License 
 */

class Topicviews extends App_Db_Table_Abstract
{
    protected $_name 	= 'forum_topicviews';
    protected $_primary = 'id'; 
 
 	function addView($topic_id,$ip=null){ 		
 		$data = array();
 		$data['topic_id'] = $topic_id;
 		if(is_null($ip)){
 			$data['ip'] = $_SERVER['REMOTE_ADDR'];	
 		}
		$data['created_on'] = date("Y-m-d H:i:s");
 		return $this->insert($data);
 	}
 	
 	function getLastTimeViewed($topic_id,$ip=null){
 		if(is_null($ip)){
 			$ip = $_SERVER['REMOTE_ADDR'];	
 		} 	
 		$where = 'ip = "'.$ip.'" and topic_id = '.$topic_id;
 		
 		$row = $this->fetchRow($where,'created_on desc'); 		
 		if(!is_null($row)){
 			return strtotime($row->created_on);
 		} else {
 			return null;
 		}
 	}
    
}